package com.friend.uu;

import android.bluetooth.BluetoothHeadset;
import android.bluetooth.BluetoothProfile;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.KeyEvent;

public class MediaEventReceiver extends BroadcastReceiver {

    private ReceivedListener mReceiverListener;

    public enum MusicCtrl {
        playOrPause,
        pause,
        next,
        previous
    }

    public MediaEventReceiver(){}

    MediaEventReceiver(ReceivedListener listener) {
        mReceiverListener = listener;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action == null || mReceiverListener == null) return;
        switch (action) {
            // 多媒体按钮
            case Intent.ACTION_MEDIA_BUTTON:
                KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
                handleMediaKeyEvent(event);
                break;
            // 耳机连接状态
            case Intent.ACTION_HEADSET_PLUG:
                if (intent.getIntExtra("state", 0) != 0) {
                    break;
                }
                // 蓝牙连接状态
            case BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED:
                if (BluetoothProfile.STATE_DISCONNECTED ==
                        intent.getIntExtra(BluetoothProfile.EXTRA_STATE, 0)) {
                    mReceiverListener.onReceived(MusicCtrl.pause);
                }
                break;
        }
    }

    private Handler mHandler = new Handler();
    private int count = 0;

    private void handleMediaKeyEvent(KeyEvent event) {
        if (event.getAction() != KeyEvent.ACTION_UP) {
            return;
        }
        switch (event.getKeyCode()) {

            case KeyEvent.KEYCODE_MEDIA_PAUSE:
            case KeyEvent.KEYCODE_MEDIA_PLAY:
                mReceiverListener.onReceived(MusicCtrl.playOrPause);
                break;
            case KeyEvent.KEYCODE_MEDIA_NEXT:
                mReceiverListener.onReceived(MusicCtrl.next);
                break;
            case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
                mReceiverListener.onReceived(MusicCtrl.previous);
                break;
            case KeyEvent.KEYCODE_HEADSETHOOK:
                if (++count == 1) {
                    mHandler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            if (count == 1) {
                                mReceiverListener.onReceived(MusicCtrl.playOrPause);
                            } else if (count == 2) {
                                mReceiverListener.onReceived(MusicCtrl.next);
                            } else if (count == 3) {
                                mReceiverListener.onReceived(MusicCtrl.previous);
                            }
                            count = 0;
                        }
                    }, 1000);
                }
                break;
        }
    }

    interface ReceivedListener {
        void onReceived(MusicCtrl ctrl);
    }
}
